package com.cskaoyan.user.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserVerifyRequest;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @author Can
 * @since 2022/07/08 23:27
 */
@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;

    /**
     * 验证激活
     * 修改 verify表和 member表中的状态
     *
     * @param request
     * @return com.cskaoyan.user.dto.UserVerifyResponse
     * @author Can
     * @since 2022/07/09 11:03
     */
    @Override
    public UserVerifyResponse  verify(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            request.requestCheck();
            // 修改verify表
            Example example = new Example(UserVerify.class);
            example.createCriteria().andEqualTo("uuid", request.getUuid());
            UserVerify verify = new UserVerify();
            verify.setIsVerify("Y");
            verify.setUuid(request.getUuid());
            userVerifyMapper.updateByExampleSelective(verify, example);

            // 修改member表
            Example memberExample = new Example(Member.class);
            memberExample.createCriteria().andEqualTo("username", request.getUsername());
            Member member = new Member();
            member.setIsVerified("Y");
            member.setUsername(request.getUsername());
            memberMapper.updateByExampleSelective(member, memberExample);

            response.setCode(UserRetCode.SUCCESS.getCode());
            response.setMsg(UserRetCode.SUCCESS.getMessage());
            log.info("用户验证激活");
        } catch (Exception e) {
            log.error("验证激活出现了异常：" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
